<?php
    /**
    * Mobile Connectors
    * Documents queries
    * 
    * @author trungdt
    * @since Dec 10, 2012
    * @copyright ABSoft Viet Nam
    * 
    * Input
    * Required
    * - session_id
    * Optional
    * - search : string
    * - datefrom : m/d/Y
    * - dateto : m/d/y
    * - filetype : none | nofile | any | pdf | ppt | xls | video | doc | xls | mp3
    * - page : 
    * - rows : int ; how many items per page
    *  
    * Output
    * {
    *   status : OK | NOT_VALID
    *   total : int
    *   rows : documents object array
    * }
    */

    global $db;
    // include the mobile connectors
    include_once(BASE."modules/profilemodule/mobile_connector.php");

    $ret = null; // init object
    $ret->status = "NOT_VALID";
    $ret->total = 0;
    $ret->rows=array();

    if (isset($_POST['session_id']))
    {
        $session_id = _ab_safe_html_string($_POST['session_id']);

        // check session is still valid or not
        // all timedout session had been wiped
        $msession = $db->selectObject("profilemodule_mobilesession", "session_id = '{$session_id}'");

        if (isset($msession->id))
        {
            // session still valid, 
            profile_module_updateClientSession_last_response($session_id);
            
            $ret->status="OK";
            $user_id=$msession->user_id;

            $sql_where = "user_id = {$msession->user_id} ";

            // Build condition for filter
            if (isset($_POST['search']))
                $sql_where.=" AND (name LIKE '%{$_POST['search']}%')";

            if (isset($_POST['datefrom']) && $_POST['datefrom']!="")
            {
                $date_from=strtotime($_POST['datefrom']);
                $sql_where.=" AND (post_date>={$date_from})";
            }

            if (isset($_POST['dateto']) && $_POST['dateto']!="")
            {
                $date_to=strtotime($_POST['dateto']);
                $sql_where.=" AND (post_date<={$date_to})";
            }

            if (isset($_POST['filetype']))
            {
                $type=$_POST['filetype'];
                if ($type == "none")
                    $sql_where.=" ";
                if ($type == "any")
                    $sql_where.=" AND (flv_file <> '' OR pdf_file <>'' OR mp3_file <>'' OR doc_file <>'' OR xls_file <>'' OR ppt_file <>'')";
                else if ($type == "pdf")
                        $sql_where.=" AND (pdf_file <>'')";
                    else if ($type == "ppt")
                            $sql_where.=" AND (ppt_file <>'')";
                        else if ($type == "video")
                                $sql_where.=" AND (flv_file <>'')";
                            else if ($type == "doc")
                                    $sql_where.=" AND (doc_file <>'')";
                                else if ($type == "xls")
                                        $sql_where.=" AND (xls_file <>'')";
                                    else if ($type == "mp3")
                                            $sql_where.=" AND (mp3_file <>'')";
                                        else if ($type == "nofile")
                                                $sql_where.=" AND (flv_file = '') AND (pdf_file ='') AND ( mp3_file ='') AND (doc_file ='') AND (xls_file ='') AND (ppt_file ='')";
            }

            $page=1;
            if (isset($_POST['page']))
                $page=intval($_POST['page']);
            $pageSize=10;

            if (isset($_POST['rows']))
                $pageSize=intval($_POST['rows']);
            $start=($page-1)*$pageSize;

            $data=$db->selectObjects('profilemodule_documents',"{$sql_where} {$sort} LIMIT {$start},{$pageSize}");

            // preprocess some data
            foreach ($data as $k=>$v){
                $v->post_date_text = date('m/d/Y H:i:s',$v->post_date);
            }

            $ret->total = $db->countObjects('profilemodule_documents',"{$sql_where}");
            $ret->rows = $data; 
            $ret->status="OK";
        }
        else
        {
            $ret->status="NOT_VALID";
        }
    }

    echo json_encode($ret);
    die();
?>